using Coding4Fun.Phone.Controls;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MicroMsg.UI.Page
{
	public class TimelineDetailPage : BasePage
	{
		private const string TAG = "TimelineDetailPage";
		private TimelineDetailCtrl mTimeLineCtrl;
		private SnsInfo mSnsInfo;
		private bool mBIsCmtBack;
		private SnsComment mCmtStrangerBack;
		private EventWatcher mWatchSnsInfo;
		private object mObjectWatcher = new object();
		private SnsComment mCmtObjToReply;
		private SnsComment mCmtToDel;
		private bool mBIsLockBackKey;
		private double mSipHeight;
		private UserHeadCtrl mHeadCtrl;
		private bool mBIsInputerInited;
		private RoundButton btnLike;
		private bool isInputFocus;
		internal Grid LayoutRoot;
		internal Rectangle rectCmtBack;
		internal ScrollViewer scrollView;
		internal Grid gridTimeline;
		internal MMInputer mInputer;
		private bool _contentLoaded;
		public TimelineDetailPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("TimelineDetailPage", "Page_Loaded");
			base.BeginListenForSIPChanged();
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("TimelineDetailPage", "Page_Unloaded");
			base.EndListenForSipChanged();
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("TimelineDetailPage", "OnPageIn");
			if (object_0 != null && object_0.Length != 0 && object_0[0] is SnsInfo)
			{
				List<SnsComment> list = null;
				if (object_0.Length == 2 && object_0[1] is List<SnsComment>)
				{
					list = (object_0[1] as List<SnsComment>);
					if (list != null && list.get_Count() != 0)
					{
						this.mCmtStrangerBack = list.get_Item(0);
					}
					this.mBIsCmtBack = true;
				}
				this.registerEvent();
				this.mTimeLineCtrl = TimelineDetailCtrl.GetOneItem();
				this.mTimeLineCtrl.mCmtItemClickedDlg = new MMStandardUIEvent(this.cmtItemClicked);
				this.mTimeLineCtrl.SetDefaultCmts(list);
				this.mTimeLineCtrl.ItemObject = object_0[0];
				this.mTimeLineCtrl.RefreshItem();
				this.mSnsInfo = (object_0[0] as SnsInfo);
				if (this.isPrivate())
				{
					this.setPrivate();
				}
				this.trySetErrorInfo();
				if (list == null && (this.mSnsInfo.commentList == null || this.mSnsInfo.commentList.list == null || this.mSnsInfo.commentList.list.get_Count() == 0) && (this.mSnsInfo.likeList == null || this.mSnsInfo.likeList.list == null || this.mSnsInfo.likeList.list.get_Count() == 0))
				{
					this.rectCmtBack.set_Visibility(1);
				}
				else
				{
					this.rectCmtBack.set_Visibility(0);
				}
				this.gridTimeline.get_Children().Add(this.mTimeLineCtrl);
				this.initInputer();
				if (this.mSnsInfo.nLikeFlag == 0)
				{
					this.btnLike.set_IsEnabled(true);
					this.btnLike.ImageSource = new BitmapImage(new Uri("/Source/Plugin/Sns/Common/Resource/like.png", 2));
				}
				else
				{
					if (this.mSnsInfo.nLikeFlag == 1)
					{
						this.btnLike.set_IsEnabled(false);
						this.btnLike.ImageSource = new BitmapImage(new Uri("/Source/Plugin/Sns/Common/Resource/liked.png", 2));
					}
				}
				this.resetReplyStage();
				this.mHeadCtrl.ItemObject = AccountMgr.strUsrName;
				this.scrollView.add_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.scrollView_ManipulationStarted));
				return;
			}
			base.GoBack();
		}
		private void scrollView_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			this.mInputer.SwitchMode(ChatInputMode.InputDefault, false);
			this.resetReplyStage();
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("TimelineDetailPage", "OnPageOut");
			this.unRegisterEvent();
			this.gridTimeline.get_Children().Clear();
			if (this.mTimeLineCtrl != null)
			{
				this.mTimeLineCtrl.mCmtItemClickedDlg = null;
			}
			TimelineDetailCtrl.PushOneItem(this.mTimeLineCtrl);
			this.mTimeLineCtrl = null;
			this.mSnsInfo = null;
		}
		protected override void OnPageBack()
		{
			Log.smethod_5("TimelineDetailPage", "OnPageBack");
		}
		private void registerEvent()
		{
			if (this.mWatchSnsInfo == null)
			{
				this.mWatchSnsInfo = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnSnsInfo));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsInfo != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
				this.mWatchSnsInfo = null;
			}
		}
		private void OnSnsInfo(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (this.mBIsCmtBack)
			{
				return;
			}
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_STORAGE_SNSINFO_MODIFY)
			{
				return;
			}
			if (this.mTimeLineCtrl != null && this.mTimeLineCtrl.ItemObject is SnsInfo)
			{
				SnsInfo snsInfo = this.mTimeLineCtrl.ItemObject as SnsInfo;
				List<SnsInfo> listObject = evtArgs.getListObject<SnsInfo>();
				if (listObject != null)
				{
					using (List<SnsInfo>.Enumerator enumerator = listObject.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							SnsInfo current = enumerator.get_Current();
							if (current != null && current.strObjectID == snsInfo.strObjectID)
							{
								this.mTimeLineCtrl.ItemObject = current;
								this.mTimeLineCtrl.RefreshItem();
							}
						}
					}
				}
				return;
			}
		}
		private void deleteCmt(SnsComment snsComment_0)
		{
			if (snsComment_0 != null && this.mTimeLineCtrl != null && this.mTimeLineCtrl.ItemObject != null && snsComment_0.nCommentId != 0)
			{
				this.mCmtToDel = snsComment_0;
				MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Album_ConfirmDelCmt, "", true, strings.COM_Confirm, strings.COM_Cancel);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(this.delCmtDialogReturn);
				return;
			}
		}
		public void delCmtDialogReturn(DlgReturnEventArg dlgReturnEventArg_0)
		{
			if (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0) == 0)
			{
				if (this.mTimeLineCtrl == null || this.mTimeLineCtrl.ItemObject == null)
				{
					return;
				}
				SnsInfo snsInfo = this.mTimeLineCtrl.ItemObject as SnsInfo;
				if (snsInfo == null)
				{
					return;
				}
				SnsInfoMgr.deleteComment(snsInfo.nObjectID, this.mCmtToDel.nCommentId, true);
				this.mTimeLineCtrl.DelComment(this.mCmtToDel.nCommentId);
			}
			this.mCmtToDel = null;
		}
		private void clickCmt(SnsComment snsComment_0)
		{
			if (snsComment_0 == null)
			{
				return;
			}
			if (this.isPrivate())
			{
				return;
			}
			if (snsComment_0.strUserName == AccountMgr.strUsrName)
			{
				return;
			}
			if (this.mSipHeight <= 1.0)
			{
				if (this.mInputer.GetMode() != ChatInputMode.InputSmileyMode)
				{
					string displayName = ContactHelper.getDisplayName(snsComment_0.strUserName, snsComment_0.strNickName);
					this.mInputer.inputTxt.HintText = strings.WC_COMMENT_TO_TA + displayName;
					base.get_Dispatcher().BeginInvoke(delegate
					{
						this.mInputer.SwitchMode(ChatInputMode.InputTxtMode, false);
					});
					this.mCmtObjToReply = snsComment_0;
					if (this.mTimeLineCtrl != null)
					{
						this.mTimeLineCtrl.set_IsEnabled(false);
					}
					return;
				}
			}
		}
		private void cmtItemClicked(object opKind, object object_0)
		{
			switch ((SnsCmtsOpKind)opKind)
			{
			case SnsCmtsOpKind.Kind_ClickLayout:
				this.clickCmt(object_0 as SnsComment);
				return;
			case SnsCmtsOpKind.Kind_Del:
				this.deleteCmt(object_0 as SnsComment);
				return;
			default:
				return;
			}
		}
		private void scrollView_ManipulationStarted_1(object sender, ManipulationStartedEventArgs e)
		{
			this.mInputer.SwitchMode(ChatInputMode.InputDefault, false);
		}
		public void ShowBigPic(CirclePagePicsCtrl picsCtrl, int nPicIndex)
		{
			if (picsCtrl == null)
			{
				return;
			}
			WriteableBitmap backgroud = new WriteableBitmap(this.LayoutRoot, null);
			this.ShowBigPic(new ScrollPicsCtrlArgs(picsCtrl, nPicIndex, null, new MMUIEvent(this.scrollPicEnded), backgroud, null));
		}
		private object scrollPicEnded(object obj1, object obj2)
		{
			this.mBIsLockBackKey = false;
			return null;
		}
		public void ShowBigPic(ScrollPicsCtrlArgs scrollPicsCtrlArgs_0)
		{
			if (!ScrollPicsCtrlArgs.CheckIsInfoOk(scrollPicsCtrlArgs_0))
			{
				return;
			}
			this.OnClearTransition();
			ScrollPicListPage.GotoThisPage(scrollPicsCtrlArgs_0, this.LayoutRoot);
		}
		private void resetReplyStage()
		{
			if (this.mTimeLineCtrl != null)
			{
				this.mTimeLineCtrl.set_IsEnabled(true);
			}
			this.mInputer.inputTxt.HintText = "";
			this.mCmtObjToReply = null;
		}
		private void scrollToEnd()
		{
			this.scrollView.ScrollToVerticalOffset(this.scrollView.get_ScrollableHeight());
		}
		private void scrollToStart()
		{
			this.scrollView.ScrollToVerticalOffset(0.0);
		}
		private void setPrivate()
		{
			this.hideInputer();
		}
		private void trySetErrorInfo()
		{
			if (this.mSnsInfo == null)
			{
				return;
			}
			if (this.mSnsInfo.nStatus == 4)
			{
				this.hideInputer();
			}
		}
		private void hideInputer()
		{
			this.mInputer.set_Visibility(1);
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (this.mBIsLockBackKey)
			{
				cancelEventArgs_0.set_Cancel(true);
				return;
			}
			if (this.mInputer.GetMode() == ChatInputMode.InputSmileyMode)
			{
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		public static void GotoThisPage(SnsInfo snsInfo, List<SnsComment> listComment = null)
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Common/UI/TimelineDetailPage.xaml", new object[]
			{
				snsInfo,
				listComment
			}, false);
		}
		private bool isPrivate()
		{
			if (this.mSnsInfo != null)
			{
				TimeLineObject timeLine = SnsInfoMgr.getTimeLine(this.mSnsInfo);
				if (timeLine != null && timeLine.nPrivate == 1)
				{
					return true;
				}
			}
			return false;
		}
		protected override void RootFrameChange(double double_0)
		{
			this.mSipHeight = double_0;
		}
		private void btnLike_Click(object sender, RoutedEventArgs e)
		{
			SnsInfo snsInfo = this.mSnsInfo;
			if (snsInfo != null)
			{
				if (snsInfo.nLikeFlag != 1)
				{
					this.btnLike.ImageSource = new BitmapImage(new Uri("/Source/Plugin/Sns/Common/Resource/liked.png", 2));
					if (SnsCommentServiceCenter.snsCommentService == null)
					{
						SnsCommentServiceCenter.init();
					}
					SnsCommentServiceCenter.snsCommentService.doSendComment(snsInfo, "", CommentType.MMSNS_COMMENT_LIKE, AddContactScene.MM_ADDSCENE_PF_CONTACT, null, -1);
					snsInfo.nLikeFlag = 1;
					this.btnLike.set_IsEnabled(false);
					SnsComment snsComment = new SnsComment();
					snsComment.strUserName = AccountMgr.strUsrName;
					DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0);
					snsComment.nCreateTime = (uint)DateTime.get_Now().Subtract(dateTime).get_TotalSeconds();
					this.mTimeLineCtrl.AddLike(snsComment);
					base.get_Dispatcher().BeginInvoke(delegate
					{
						this.scrollToStart();
					});
					return;
				}
			}
		}
		private void DoSend(string realTxt)
		{
			if (string.IsNullOrWhiteSpace(realTxt))
			{
				base.Focus();
				this.resetReplyStage();
				MessageBoxManager.ShowToast(strings.ChatEmptyTxtHint, 2500, null, false);
				return;
			}
			SnsInfo snsInfo = this.mSnsInfo;
			int source = 6;
			if (snsInfo == null)
			{
				return;
			}
			if (SnsCommentServiceCenter.snsCommentService == null)
			{
				SnsCommentServiceCenter.init();
			}
			SnsComment snsComment = new SnsComment();
			snsComment.nCommentId = 0;
			snsComment.strUserName = AccountMgr.strUsrName;
			snsComment.strContent = realTxt;
			if (this.mCmtObjToReply != null)
			{
				snsComment.nReplyCommentId = this.mCmtObjToReply.nCommentId;
				snsComment.strReplyUsername = this.mCmtObjToReply.strUserName;
				source = (int)this.mCmtObjToReply.nSource;
			}
			else
			{
				snsComment.nReplyCommentId = 0;
				snsComment.strReplyUsername = "";
			}
			CommentType type = CommentType.MMSNS_COMMENT_TEXT;
			CommentArg commentArg = null;
			if (this.mCmtObjToReply != null)
			{
				if (commentArg == null)
				{
					commentArg = new CommentArg();
				}
				commentArg.commentID = snsComment.nReplyCommentId;
				commentArg.strUserName = snsComment.strReplyUsername;
				commentArg.strNickName = this.mCmtObjToReply.strNickName;
			}
			if (this.mBIsCmtBack && this.mCmtStrangerBack != null)
			{
				if (commentArg == null)
				{
					commentArg = new CommentArg();
				}
				type = CommentType.MMSNS_COMMENT_MESSAGE;
				snsComment.strReplyUsername = this.mCmtStrangerBack.strUserName;
				snsComment.nReplyCommentId = this.mCmtStrangerBack.nCommentId;
				source = (int)this.mCmtStrangerBack.nSource;
				commentArg.commentID = this.mCmtStrangerBack.nCommentId;
				commentArg.strUserName = this.mCmtStrangerBack.strUserName;
				commentArg.strNickName = this.mCmtStrangerBack.strNickName;
			}
			SnsCommentServiceCenter.snsCommentService.doSendComment(snsInfo, realTxt, type, (AddContactScene)source, commentArg, -1);
			snsComment.nCreateTime = (uint)Util.getNowSeconds();
			this.mTimeLineCtrl.AddComment(snsComment);
			this.mTimeLineCtrl.UpdateLayout();
			base.UpdateLayout();
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.RefreshPushUp();
			});
			this.resetReplyStage();
		}
		private void initInputer()
		{
			if (this.mBIsInputerInited)
			{
				return;
			}
			this.mBIsInputerInited = true;
			this.mInputer.VoiceSwitchBtn.set_Visibility(1);
			this.mInputer.gridInputArea.set_Width(267.0);
			this.mHeadCtrl = new UserHeadCtrl();
			this.mHeadCtrl.set_Width(53.0);
			this.mHeadCtrl.set_Height(53.0);
			this.mHeadCtrl.set_Margin(new Thickness(20.0, 0.0, 0.0, 0.0));
			this.mHeadCtrl.ItemObject = AccountMgr.strUsrName;
			this.mInputer.inputBk.get_Children().Insert(0, this.mHeadCtrl);
			this.btnLike = new RoundButton();
			this.btnLike.Orientation = 1;
			RoundButton arg_102_0 = this.btnLike;
			BitmapImage bitmapImage = new BitmapImage();
			bitmapImage.set_UriSource(new Uri("/Source/Plugin/Sns/Common/Resource/liked.png", 2));
			arg_102_0.ImageSource = bitmapImage;
			this.btnLike.set_Content("Image");
			this.btnLike.set_Width(71.0);
			this.btnLike.set_Height(53.0);
			Control arg_157_0 = this.btnLike;
			SolidColorBrush solidColorBrush = new SolidColorBrush();
			solidColorBrush.set_Color(Colors.get_White());
			arg_157_0.set_Foreground(solidColorBrush);
			Control arg_174_0 = this.btnLike;
			SolidColorBrush solidColorBrush2 = new SolidColorBrush();
			solidColorBrush2.set_Color(Colors.get_White());
			arg_174_0.set_BorderBrush(solidColorBrush2);
			this.btnLike.set_VerticalAlignment(2);
			this.btnLike.set_HorizontalAlignment(1);
			this.btnLike.add_Click(new RoutedEventHandler(this.btnLike_Click));
			this.btnLike.set_Margin(new Thickness(0.0, 0.0, 0.0, 9.0));
			this.mInputer.inputBk.get_Children().Add(this.btnLike);
			this.mInputer.inputTxt.mBShowHintWhenEmpty = true;
			this.mInputer.bIsWithoutArtSmiley = true;
			this.mInputer.UpdateLayout();
		}
		private object mInputer_mDlgBtnSendText(object sender, object object_0)
		{
			this.DoSend(object_0 as string);
			return null;
		}
		private object mInputer_mDlgInputAtMember(object sender, object object_0)
		{
			return null;
		}
		private void RefreshPushUp()
		{
			if (this.mInputer.IsPagePopUped())
			{
				if (this.scrollView.get_ExtentHeight() < this.scrollView.get_Height())
				{
					double num = this.scrollView.get_Height() - this.scrollView.get_ExtentHeight();
					if (num > 0.0)
					{
						this.scrollView.set_Margin(new Thickness(0.0, num, 0.0, 0.0));
					}
					else
					{
						this.scrollView.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
					}
				}
				else
				{
					this.scrollView.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
				}
			}
			else
			{
				this.scrollView.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
			}
			this.scrollToEnd();
		}
		private object mInputer_mDlgOnEndPushPage(object sender, object object_0)
		{
			this.RefreshPushUp();
			return null;
		}
		private object mInputer_mDlgOnInputerModeChange(object sender, object object_0)
		{
			return null;
		}
		private object mInputer_mDlgOnRealBoxGotFocus(object sender, object object_0)
		{
			this.isInputFocus = true;
			return null;
		}
		private object mInputer_mDlgOnRealBoxLostFocus(object sender, object object_0)
		{
			this.isInputFocus = false;
			return null;
		}
		private object mInputer_mDlgOnStartPushPage(object sender, object object_0)
		{
			this.RefreshPushUp();
			return null;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/TimelineDetailPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.rectCmtBack = (Rectangle)base.FindName("rectCmtBack");
			this.scrollView = (ScrollViewer)base.FindName("scrollView");
			this.gridTimeline = (Grid)base.FindName("gridTimeline");
			this.mInputer = (MMInputer)base.FindName("mInputer");
		}
	}
}
